// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;

namespace SnapObjects.Data
{
    internal abstract class QueryBuilderParser : SqlBuilderParser
    {
        public QueryBuilderParser(
            ISqlStatementFactory factory,
            ISqlGenerationHelper helper,
            ITypeMapper typeMapper) : base(factory, helper, typeMapper)
        {
        }

        public override SqlStatement Parse(SqlBuildStatement statement)
        {
            var select = (SelectBuildStatement)statement;

            var stmt = new SqlSelectStatement(select.IgnoreDelimitIdentifier);

            stmt.Take = select.Take;
            stmt.Skip = select.Skip;
            stmt.IsDistinst = select.IsDistinct;

            this.ParseBuildParameters(select);

            if (!this.ParseClause(
                select, stmt.Select, select.Select, new SqlCharacterToken(SqlCharacters.Comma)))
            {
                throw new InvalidCastException("Failed to parse the select column query expression.");
            }

            if (!this.ParseClause(
                select, stmt.From, select.From, SqlWhitespaceToken.Whitespace))
            {
                throw new InvalidCastException("Failed to parse the from clause expression.");
            }

            this.ParseClause(
                select, stmt.Where, select.Where, SqlWhitespaceToken.Whitespace);

            this.ParseClause(
                select, stmt.GroupBy, select.GroupBy, new SqlCharacterToken(SqlCharacters.Comma));

            this.ParseClause(
                select, stmt.Having, select.Having, SqlWhitespaceToken.Whitespace);

            this.ParseClause(
                select, stmt.OrderBy, select.OrderBy, new SqlCharacterToken(SqlCharacters.Comma));

            this.ParseUnions(select, stmt.Unions, select.Unions);

            return stmt;
        }

        protected virtual bool ParseUnions(SqlBuildStatement buildStatement,
            SqlUnionClause union, IList<SqlElement> unions)
        {
            if (unions.Count > 0)
            {
                foreach (var item in unions)
                {
                    var uitem = (UnionElement)item;

                    SqlSelectStatement statement;

                    if (uitem.Statement is RawBuildStatement raw && raw.SyntaxType == SqlStatementType.Select)
                    {
                        raw.AddParameters(buildStatement.Parameters);

                        statement = (SqlSelectStatement)this.ParseStatement(raw);
                    }
                    else if (uitem.Statement is SelectBuildStatement select)
                    {
                        statement = (SqlSelectStatement)this.ParseStatement(uitem.Statement);
                    }
                    else
                    {
                        throw new NotSupportedException(uitem.Statement.StatementType.ToString());
                    }

                    union.Add(statement, uitem.UnionType);
                }

                return true;
            }

            return false;
        }
    }
}
